package com.youmans.userservice.website;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class WebsiteApplication {


    public static void main(String[] args) {
        getAddressByIp("39.98.72.193");
    }
    // 测试类根据IP获取地址
    public static String  getAddressByIp(String ip){
        // 参数ip
//        String ip = "218.201.8.37";
        // json_result用于接收返回的json数据
//        boolean b = IpUtils.internalIp(ip);
        // 内网不查询
        String loginAddress = "" ;
        if (IpUtils.internalIp(ip)) {
             loginAddress = "内网IP";
        }
        String json_result = null;
        try {
            json_result = AddressUtils.getAddresses("ip=" + ip + "&accessKey=alibaba-inc", "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        //使用fastjson处理json数据
        JSONObject json = JSON.parseObject(json_result);
        if(json != null) {
            String country = json.getJSONObject("data").get("country").toString();
            //String area= json.getJSONObject("data").get("area").toString();
            String province = json.getJSONObject("data").get("region").toString();
            String city = json.getJSONObject("data").get("city").toString();
            //String county= json.getJSONObject("data").get("county").toString();
            String isp = json.getJSONObject("data").get("isp").toString();


            System.out.println("国家： " + country);
            //System.out.println("地区： " + area);
            System.out.println("省份: " + province);
            System.out.println("城市： " + city);
            //System.out.println("区/县： " + county);
            System.out.println("互联网服务提供商： " + isp);
            loginAddress = province+city ;
        }

        System.err.println(loginAddress);
        return loginAddress ;
    }
}
